package com.testmanager.testmananger.shuiyou.common.service;


import com.alibaba.fastjson.JSONObject;
import com.testmanager.testmananger.foundation.dto.ConstantDto;
import com.testmanager.testmananger.foundation.mapper.ConstantMapper;
import com.testmanager.testmananger.shuiyou.common.Constant.DbSql;
import com.testmanager.testmananger.shuiyou.common.Constant.RedisFileName;
import com.testmanager.testmananger.shuiyou.common.Constant.SqlEnv;
import com.testmanager.testmananger.shuiyou.common.dto.*;
import com.testmanager.testmananger.shuiyou.common.util.JUDANRedisFileManager;
import com.testmanager.testmananger.shuiyou.common.util.OtherDataSource;
import com.testmanager.testmananger.shuiyou.common.util.OtherRedis;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Slf4j
@Service
public class OtherDbOperaService {

    @Autowired
    OtherDataSource otherDataSource;

//    @Resource(name = "redisOther")
//    RedisTemplate<String,Object> redisTemplate;
//
//    @Resource(name = "juduanFilesJSONObject")
//    JSONObject redisjsonFiles;

    @Autowired
    JUDANRedisFileManager judanRedisFileManager;
    @Autowired
    OtherRedis otherRedis;


    /**
     *  删除对应记录
     */


    /**
     * 修改对应记录
     */

    /**
     * 查询对应记录
     */

    /**
     * 在服务端处理rediskey，redis 原始值 ，修改值 等
     */
    public ResponseEntity getRedisOriginValue(String expression ,String filename ){

        return null;

    }


    /**
     * 查询redis记录 以304 作为模版
     */
    public ResponseEntity getRedisFile(String nsrid){
        return ResponseEntity.ok(judanRedisFileManager.getRedisFIlesJSONObject());
       // return null;

    }

    /**
     * 更新redis记录，存在对应的key刚更新 ，不存在则创建，可以直接覆盖
     */
    public ResponseEntity updateRedisContent(String nsrid,JSONObject changedJsonFiles){
       String base = judanRedisFileManager.jsonToStrOfJufan(changedJsonFiles.getJSONObject(RedisFileName.BASE));
       String cwbb = judanRedisFileManager.jsonToStrOfJufan(changedJsonFiles.getJSONObject(RedisFileName.CWBB));
       String base_name = RedisFileName.getBaseName(nsrid);
       String cwbb_name = RedisFileName.getCwbbName(nsrid);
       otherRedis.getRedisTemplate().opsForValue().set(base_name,base);
       otherRedis.getRedisTemplate().opsForValue().set(cwbb_name,cwbb);

        return ResponseEntity.ok().build();
    }



    /**
     * 查询规则对应信息
     * @return
     */
    public ResponseEntity getRuleInfo(String ruleid,int sqlenv){
        SqlEnv sqlEnv = SqlEnv.getValueOf(sqlenv);


        List<SdssPreRuleScriptDto> prerules = otherDataSource.getSelectResult(sqlEnv, DbSql.getSearchpreruleSql(ruleid), SdssPreRuleScriptDto.class);
       List<SdssbjkRuleScriptDto> mainrules = otherDataSource.getSelectResult(sqlEnv, DbSql.getSearchMainRuleSql(ruleid), SdssbjkRuleScriptDto.class);
        List<SdssbjkRuleVarDto> varVos = otherDataSource.getSelectResult(sqlEnv, DbSql.getSearchvarvoSql(ruleid), SdssbjkRuleVarDto.class);
        SdssRuleInfosDto ruleInfosDto = new SdssRuleInfosDto();
        ruleInfosDto.setPreRuleScriptDtos(prerules)
                .setRuleScriptDtos(mainrules)
                .setRuleVarDtos(varVos);


        // String sql = DbSql.get
       // otherDataSource.getSelectResult(sqlEnv,)
        return ResponseEntity.ok(ruleInfosDto);
    }

    public ResponseEntity getRuleResult(String ruleid , String nsrid){
        List SdssRuleResultDtos = otherDataSource.getSelectResult(SqlEnv.JUFAN, DbSql.getSearchRuleResult(ruleid,nsrid), SdssRuleResultDto.class);
        log.info(SdssRuleResultDtos.toString());
        return ResponseEntity.ok(SdssRuleResultDtos);
    }




    public List<ConstantDto> getSqlEnvs(){
        List<ConstantDto> result = new ArrayList<>();
        Arrays.stream(SqlEnv.values())
                .forEach(t -> result.add(ConstantMapper.toConstantDto(t)));

        return result;
    }
}
